*******************************************************************************
* Loop over subgroups
*******************************************************************************

foreach subgroup in All Democrat Republican {

*******************************************************************************
* Load data
*******************************************************************************

use dataset_gss, clear
	
*******************************************************************************
* Keep the subgroup
*******************************************************************************

keep if subgroup == "`subgroup'"

*******************************************************************************
* Label lines if it's the first plot
*******************************************************************************

local legend1 legend(off)
local legend2 legend(off)
if "`subgroup'" == "All" {
local legend1 legend(order(1 ///
	"{fontface monospace:{bf:Support Police Spending}}" ///
	2 "{fontface monospace:{bf:Support Other Spending}}") ///
	symysize(0) symxsize(0) ring(0) pos(11) cols(1) ///
	region(lwid(none) fcol(none)))
local ///
legend2 legend(order(1 "{fontface monospace:{bf:Difference}}") ///
	symysize(0) symxsize(0) ring(0) pos(11) cols(1) ///
	region(lwid(none)))
}

*******************************************************************************
* Plot raw time series
*******************************************************************************

egen natcf = mean(nat) if target != "crimy", by(year)

collapse nat*, by(target year)

levelsof target if target != "crimy", local(levels)
foreach l of local levels {
	#delimit;
	local mylines `mylines' (line nat year if target == "`l'", 
		lwid(thin) lcol(gray));
	#delimit cr
}

#delimit;

gr tw
	//`mylines'
	(line nat year if target == "crimy", lwid(thick) lcol(black))
	(line natcf year if target == "educ", lwid(thick) lcol(gray)) 
	,
		xline(2014, lpat(dash))
		`legend1'
		plotregion(style(none))
		name(g1, replace)
		xtitle("")
		ytitle("")
		ylab(-0.2(0.2)0.8, angle(horiz))
		xlab(1982(8)2022)
		;
		
#delimit cr

gr_edit .legend.plotregion1.label[2].style.editstyle color(gray) editcopy

*******************************************************************************
* Plot differences
*******************************************************************************

egen natpolicem = mean(nat) if target == "crimy", by(year)
egen natpolice = mean(natpolicem), by(year)

gen d = natpolice - natcf

#delimit;

gr tw
	(line d year if target == "educ", lwid(thick) lcol(black))
	(sc d year if target == "educ", msym(none))
	,
		xline(2014, lpat(dash))
		name(g2, replace)
		plotregion(style(none))
		ylab(
			-.6 "-.6"
			-.4 "-.4"
			-.2 "-.2"
			0 "0"
			.2 ".2"
			.4 ".4"
			.6 ".6"
			,
				angle(horiz)
		)
		`legend2'
		xtitle("")
		ytitle("")
		yline(0, lpat(shortdash) lcol(black))
		xlab(1982(8)2022)
		;
		
#delimit cr

gr close

*******************************************************************************
* Combine plots
*******************************************************************************

local mytitle `subgroup'
if "`subgroup'" != "All" local mytitle = "`subgroup's" 

gr combine g1 g2, name(G`subgroup', replace) title(`mytitle') 
gr close

*******************************************************************************
* Close subgroup loop
*******************************************************************************

}

*******************************************************************************
* Combine graphs
*******************************************************************************

gr combine GAll GDemocrat GRepublican, rows(3) ysize(4) xsize(3) ///
	imargin(zero)

*******************************************************************************
* Save
*******************************************************************************

gr export "figure_gss_dd.pdf", replace

*******************************************************************************
* End
*******************************************************************************
